/**
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.controller.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import ru.portnyagin.helpdeskru.model.Role;
import ru.portnyagin.helpdeskru.model.RoleEnum;
import ru.portnyagin.helpdeskru.model.UserHD;
import ru.portnyagin.helpdeskru.service.UserHDService;
import ru.portnyagin.helpdeskru.util.JsfUtil;

@ManagedBean(name = "usersController")
@ViewScoped
public class AdminUsersController implements Serializable {

    @EJB
    private ru.portnyagin.helpdeskru.service.UserHDService userHDService;

    private List<UserHD> listAllUsers = null;
    private UserHD currentUser = null;
    private String newUserPassword;
    private String newUserPassword2;
            
    public AdminUsersController() {
    }
    

    private UserHDService getUserHDService() {
        return userHDService;
    }

    public List<UserHD> getListAllUsers() {
        if(listAllUsers == null) {
            listAllUsers = getUserHDService().findAll();
        }
        return listAllUsers;
    }
    
    public SelectItem[] getSelectItemListRoles() {
        List<RoleEnum> l = new ArrayList();
        l.addAll(Arrays.asList(RoleEnum.values()));
        return JsfUtil.getSelectItems(l , false);
    }

    public UserHD getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(UserHD currentUser) {
        this.currentUser = currentUser;
    }

    public String getNewUserPassword() {
        return newUserPassword;
    }

    public void setNewUserPassword(String newUserPassword) {
        this.newUserPassword = newUserPassword;
    }

    public String getNewUserPassword2() {
        return newUserPassword2;
    }

    public void setNewUserPassword2(String newUserPassword2) {
        this.newUserPassword2 = newUserPassword2;
    }
    
    public void deleteUser() {
        getUserHDService().remove(currentUser);
    }
    
    public void addNewUser() {
        currentUser = new UserHD();
        currentUser.setRole(new Role());
        listAllUsers.add(0, currentUser);
    }
    
    
    public void saveCurrentUserPassword() {
        if((newUserPassword != null) && (newUserPassword.equals(newUserPassword2))) {
            currentUser.setPassword(newUserPassword);
        }
        else {
            return;
        }
        
        getUserHDService().edit(currentUser);
    }
    
    public void saveAll() {
        
        try{
            for(UserHD u: listAllUsers) {
                
                u.getRole().setLogin(u.getLogin());
                
                if(u.getId() != null) {
                    getUserHDService().edit(u);
                } else {
                    getUserHDService().create(u);
                }
            }
        } catch (Exception e){
            JsfUtil.addErrorMessage(e.toString());
        }                
        
    }
    
    

}
